package hamsterwrench.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.OutputStream;
import java.io.PrintStream;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class GrimReaper extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4282407886690339160L;
	//TODO change this so that it takes a Throwable as argument and changes System.out to the JTextArea
	private final JPanel contentPanel = new JPanel();
	JTextArea textArea;

	/*public static void main(String[] args) {
		try {
			GrimReaper dialog = new GrimReaper();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
	
	public static void panic(Exception e){
		Window[] windows = Window.getWindows();
		GrimReaper gr = new GrimReaper();
		gr.reassignErrStream();
		for(int i = 0; i < windows.length; i++){
			windows[i].dispose();
		}
		e.printStackTrace();
		gr.setVisible(true);
	}

	/**
	 * Create the dialog.
	 */
	public GrimReaper() {
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setModalityType(ModalityType.APPLICATION_MODAL);
		setTitle("The Grim Reaper");
		setIconImage(Toolkit.getDefaultToolkit().getImage(GrimReaper.class.getResource("/com/sun/java/swing/plaf/motif/icons/Error.gif")));
		setBounds(100, 100, 800, 349);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(null);
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		FormLayout fl_contentPanel = new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.UNRELATED_GAP_COLSPEC,},
			new RowSpec[] {
				FormFactory.LINE_GAP_ROWSPEC,
				RowSpec.decode("fill:16px"),
				FormFactory.LINE_GAP_ROWSPEC,
				RowSpec.decode("fill:max(60dlu;pref):grow"),
				FormFactory.LINE_GAP_ROWSPEC,
				FormFactory.UNRELATED_GAP_ROWSPEC,});
		contentPanel.setLayout(fl_contentPanel);
		{
			JLabel lblAnErrorOccured = new JLabel("Sorry, but HamsterWrench encountered a serious error and needs to close.");
			contentPanel.add(lblAnErrorOccured, "4, 2, fill, bottom");
		}
		{
			JLabel label = new JLabel("");
			label.setIcon(new ImageIcon(GrimReaper.class.getResource("/com/sun/java/swing/plaf/windows/icons/Error.gif")));
			contentPanel.add(label, "2, 4, left, top");
		}
		{
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
			scrollPane.setViewportBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
			contentPanel.add(scrollPane, "4, 4, fill, fill");
			{
				textArea = new JTextArea();
				textArea.setBackground(UIManager.getColor("TextArea.disabledBackground"));
				textArea.setWrapStyleWord(true);
				textArea.setLineWrap(true);
				textArea.setEditable(false);
				scrollPane.setViewportView(textArea);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("Exit HamsterWrench");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
	}

	private void updateTextArea(final String text){
//		SwingUtilities.invokeLater(new Runnable(){
//			public void run(){
				textArea.append(text);
//			}
//		});
	}
	
	private void reassignErrStream(){
		OutputStream out = new OutputStream() {
			public void write(final int b){
				updateTextArea(String.valueOf((char)b));
			}
			
			public void write(byte[] b, int off, int len){
				updateTextArea(new String(b, off, len));
			}
			
			public void write(byte[] b){
				write(b, 0, b.length);
			}
		};
		
		System.setErr(new PrintStream(out, true));
	}
}
